package com.service.impl;

import java.util.List;

import com.dao.CustomerDAO;
import com.dao.PaymentDAO;
import com.dao.ProductDAO;
import com.dao.SaleOrderDAO;
import com.dao.SaleOrderItemDAO;
import com.dao.impl.CustomerDAOImpl;
import com.dao.impl.PaymentDAOImpl;
import com.dao.impl.ProductDAOImpl;
import com.dao.impl.SaleOrderDAOImpl;
import com.dao.impl.SaleOrderItemDAOImpl;
import com.model.Payment;
import com.model.SaleOrder;
import com.model.SaleOrderItem;
import com.service.SaleService;
import com.util.jsonUtil;

public class SaleServiceImpl implements SaleService {
	private ProductDAO productDAO = new ProductDAOImpl();
	private SaleOrderDAO saleOrderDAO = new SaleOrderDAOImpl();
	private SaleOrderItemDAO saleOrderItemDAO = new SaleOrderItemDAOImpl();
	private PaymentDAO paymentDAO = new PaymentDAOImpl();
	private CustomerDAO customerDAO = new CustomerDAOImpl();

	// private Product product;
	// public static List<ItemTemp> itemTempList = new ArrayList<ItemTemp>();
	// public int id = 1;

	public ProductDAO getProductDAO() {
		return productDAO;
	}

	public void setProductDAO(ProductDAO productDAO) {
		this.productDAO = productDAO;
	}

	public SaleOrderDAO getSaleOrderDAO() {
		return saleOrderDAO;
	}

	public void setSaleOrderDAO(SaleOrderDAO saleOrderDAO) {
		this.saleOrderDAO = saleOrderDAO;
	}

	public SaleOrderItemDAO getSaleOrderItemDAO() {
		return saleOrderItemDAO;
	}

	public void setSaleOrderItemDAO(SaleOrderItemDAO saleOrderItemDAO) {
		this.saleOrderItemDAO = saleOrderItemDAO;
	}

	public PaymentDAO getPaymentDAO() {
		return paymentDAO;
	}

	public void setPaymentDAO(PaymentDAO paymentDAO) {
		this.paymentDAO = paymentDAO;
	}

	public CustomerDAO getCustomerDAO() {
		return customerDAO;
	}

	public void setCustomerDAO(CustomerDAO customerDAO) {
		this.customerDAO = customerDAO;
	}

	@Override
	public boolean addOrder(SaleOrder saleOrder) {
		// TODO Auto-generated method stub
		return saleOrderDAO.addOrder(saleOrder);
	}

	@Override
	public boolean addOrderItem(SaleOrderItem saleOrderItem) {
		// TODO Auto-generated method stub
		return saleOrderItemDAO.addSaleOrderItem(saleOrderItem);
	}

	@Override
	public boolean addPayment(Payment payment) {
		// TODO Auto-generated method stub
		return paymentDAO.addPayment(payment);
	}

	@Override
	public String selectSaleOrder(String key, String type, int page, int rows,
			String sort, String order) {
		// TODO Auto-generated method stub
		List<?> list = saleOrderDAO.selectSaleOrder(key, type, page, rows,
				sort, order);
		int total;
		if (key != null) {
			total = saleOrderDAO.getLikeTotal(key, type);
			return jsonUtil.toJson(list, total);
		} else {
			total = saleOrderDAO.getTotal();
			return jsonUtil.toJson(list, total);
		}

	}

	@Override
	public int getTotal() {
		// TODO Auto-generated method stub
		return saleOrderDAO.getTotal();
	}

	@Override
	public int getLikeTotal(String key, String type) {
		// TODO Auto-generated method stub
		return saleOrderDAO.getLikeTotal(key, type);
	}

	@Override
	public int getLastNum() {
		// TODO Auto-generated method stub
		return saleOrderDAO.getLastNum();
	}

	/*
	 * @Override public List<?> enterProduct(String productNo, int quantity,
	 * double discount) { // TODO Auto-generated method stub DecimalFormat df =
	 * new DecimalFormat(".##"); product = productDAO.getProduct(productNo);
	 * double subtotal = product.getPrice() * quantity * discount; subtotal =
	 * Double.parseDouble(df.format(subtotal)); ItemTemp itemTemp = new
	 * ItemTemp(); itemTemp.setId(id);
	 * itemTemp.setProductNo(product.getProductNo());
	 * itemTemp.setProductName(product.getProductName());
	 * itemTemp.setPrice(product.getPrice()); itemTemp.setDiscount(discount);
	 * itemTemp.setSubtotal(subtotal); itemTemp.setQuantity(quantity);
	 * itemTempList.add(itemTemp); id++; return itemTempList; }
	 */

	@Override
	public boolean sale(SaleOrder saleOrder, SaleOrderItem saleOrderItem,
			Payment payment) {
		// TODO Auto-generated method stub
		boolean b = addOrder(saleOrder) && addOrderItem(saleOrderItem)
				&& addPayment(payment);
		return b;
	}

	@Override
	public int getPaymentNum() {
		// TODO Auto-generated method stub
		return paymentDAO.getLastNum();
	}

}
